/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package IsdComponent;

import com.example.CurrentDocument;
import com.sun.star.beans.PropertyValue;
import com.sun.star.document.XDocumentInsertable;
import com.sun.star.frame.XController;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XModel;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.text.XText;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextViewCursor;
import com.sun.star.text.XTextViewCursorSupplier;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Shameera
 */
public class InsertISD {
    private final XComponentContext m_xContext;
    private final XMultiComponentFactory xmcf;
    private final Object desktop;
    private final XDesktop xDesktop;
    private final XTextDocument aTextDocument;
    private final XComponent xDoc;
    private  XText xText;
    private  XTextCursor TextCursor;
    private PropertyValue[] loadProps;
    private String component = "E:/Pattern1.odt";

    public InsertISD(XComponentContext m_xContext) throws Exception {
        this.m_xContext = m_xContext;
        xmcf = m_xContext.getServiceManager();

             desktop = xmcf.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
             xDesktop = (XDesktop) UnoRuntime.queryInterface
                            (com.sun.star.frame.XDesktop.class, desktop);
             xDoc = xDesktop.getCurrentComponent();
aTextDocument = (XTextDocument)UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, xDoc);
xText = aTextDocument.getText();
      TextCursor = xText.createTextCursor();
      insertComp();
      getBookMarks();
    }


    private void insertComp(){
        loadProps = new PropertyValue[1];
      loadProps[0] = new PropertyValue();
      loadProps[0].Name = "Hidden";
      loadProps[0].Value = new Boolean(true);

      TextCursor.gotoEnd(false);
      XDocumentInsertable xd = (XDocumentInsertable) UnoRuntime
            .queryInterface(XDocumentInsertable.class, TextCursor);

       try {
         xd.insertDocumentFromURL("file:///" + component,loadProps);
      } catch (IllegalArgumentException ex) {
         System.out
               .println("No encuentra documento a insertar por fallo en ruta");
         System.out.println(ex);
      } catch (com.sun.star.io.IOException ex) {
         System.out.println("Fallo en el documento a insertar");
         System.out.println(ex);
      }
     // TextCursor.gotoEnd(false);
    }

    private void getBookMarks(){
        try {
            CurrentDocument curDoc = new CurrentDocument(m_xContext);
        } catch (Exception ex) {
            Logger.getLogger(InsertISD.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
